using Microsoft.Extensions.Logging;

namespace Vin.Extension.NLog;

public static class NLoggerExtension
{
    #region ScopeProperty ${scopeproperty:item=xxx}

    public static ILogger PushProperty(this ILogger logger, string key, object value)
    {
        global::NLog.ScopeContext.PushProperty(key, value);
        return logger;
    }

    public static void PushProperty(string key, object value)
    {
        global::NLog.ScopeContext.PushProperty(key, value);
    }

    public static ILogger PushProperty(this ILogger logger, Dictionary<string, object> properties)
    {
        foreach (var (key, value) in properties)
        {
            global::NLog.ScopeContext.PushProperty(key, value);
        }

        return logger;
    }

    public static ILogger PushPathProperty(this ILogger logger, string path)
    {
        if (string.IsNullOrEmpty(path)) return logger;
        if (!path.EndsWith("/")) path += "/";
        global::NLog.ScopeContext.PushProperty("path", path);
        return logger;
    }

    #endregion

    #region 打印日志

    public static void Trace(this ILogger logger, string tag, string? message)
    {
        logger.PushPathProperty(tag).LogTrace(message);
    }

    public static void Debug(this ILogger logger, string tag, string? message)
    {
        logger.PushPathProperty(tag).LogDebug(message);
    }

    public static void Info(this ILogger logger, string tag, string? message)
    {
        logger.PushPathProperty(tag).LogInformation(message);
    }

    public static void Warn(this ILogger logger, string tag, string? message)
    {
        logger.PushPathProperty(tag).LogWarning(message);
    }

    public static void Error(this ILogger logger, string tag, string? message)
    {
        logger.PushPathProperty(tag).LogError(message);
    }

    public static void Error(this ILogger logger, string tag, Exception exception, string? message)
    {
        logger.PushPathProperty(tag).LogError(exception, message);
    }

    public static void Fatal(this ILogger logger, string tag, string? message)
    {
        logger.PushPathProperty(tag).LogCritical(message);
    }

    public static void Fatal(this ILogger logger, string tag, Exception exception, string? message)
    {
        logger.PushPathProperty(tag).LogCritical(exception, message);
    }

    #endregion
}